<?php

namespace App\Repositories;


use App\Models\Attachment;

class AttachmentRepository
{
    /**
     * 获取附件
     *
     * @param int $id
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null|static|static[]
     */
    public function getById($id)
    {
        return Attachment::find($id);
    }

    /**
     * 创建附件记录
     *
     * @param array $attributes
     * @return mixed
     */
    public function create($attributes)
    {
        return Attachment::create($attributes);
    }

    /**
     * 获取附件分页数据
     *
     * @param string $type
     * @param int $user_id
     * @param array $with
     * @param string $order_by_column
     * @param string $order_by_direction
     * @param int $per_page
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function paginate($type, $user_id, $with = [], $order_by_column = 'id', $order_by_direction = 'desc', $per_page = 20)
    {
        $attachment = Attachment::query();
        if (is_numeric($user_id) && $user_id > 0) {
            $attachment->where('user_id', $user_id);
        }
        switch ($type) {
            case 'image':
                $attachment->image();
                break;
            case 'video':
                $attachment->video();
                break;
            case 'other':
                $attachment->other();
                break;
            default:
                $attachment->image();
        }

        return $attachment->with($with)->orderBy($order_by_column, $order_by_direction)->paginate($per_page);
    }
}